home *** CD-ROM | disk | FTP | other *** search
/ Trading on the Edge / Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin / pc / mac_file / vendor_d / azte_pro / inet.c < prev    next >
Text File  |  1993-06-11  |  4KB  |  163 lines

  1. /* 7/17/89.
  2.  * Routine to set up a socket and connect to the monitor.
  3.  * Included into dani.c, inquire.c, and into control.c if INETBASED.
  4.  * Returns 0 if OK, 1 if no game is being played.  Calls error() if error.
  5.  *
  6.  * You might possibly have to include a special library at link time to
  7.  * get this to work.  E.g. on a Cray you need -lnet.  Ask a local expert
  8.  * what library you need for 'socket' and 'connect' if you have a 
  9.  * problem.
  10.  */
  11.  
  12. extern void getpassword();
  13.  
  14. #ifdef VMS
  15. #define ERRNO uerrno
  16. #else
  17. #define ERRNO errno
  18. #endif
  19.  
  20. #define MAXNAME    32    /* must be < BUFSIZE in monitor's network.c */
  21. /*#define PORT 12345*/        /* port number for monitor */
  22. /* defined in define.h now */
  23.  
  24.  
  25. int
  26. inetsetup(hostnam,portnum,dsply)
  27. char *hostnam;
  28. int portnum;
  29. int dsply;
  30. {
  31.     int sock;
  32.     struct sockaddr_in server;
  33.     static char name[MAXNAME];
  34.     static char password[MAXNAME];
  35.     char msg[100];
  36.     char *mptr, *p;
  37.     long int hostaddr;    /* must be 32 bits */
  38.     extern int ERRNO;
  39.     struct hostent *blob;
  40.     struct in_addr *host_address;
  41.  
  42. /* Zero the whole struct sockaddr_in server.
  43.  * If you don't have bzero() and bcopy(), look for memset() and memcpy()
  44.  * respectively, but note that source and destination are reversed in
  45.  * memcpy(), and that memset() has an extra argument (set it to 0)
  46.  */
  47.     bzero((char*)&server, sizeof(server));
  48.  
  49. /* Put the network address of the DA monitor into the server structure.
  50.  * This uses the sfi.santafe.edu address unless you have the full 
  51.  * networking software (file anyhost.c) and define ANYHOST
  52.  */
  53. #ifdef ANYHOST
  54.     anyhost(&server,&hostnam);
  55. #else
  56. /*
  57.     if (hostnam != NULL) {
  58.     sprintf(msg,"can only connect to %s",MONITORHOST);
  59.     error(msg);
  60.     }
  61. */
  62.     if (hostnam == NULL)
  63.     hostnam = MONITORHOST;
  64.     if ((blob = gethostbyname(hostnam)) == NULL) {
  65.     sprintf(msg,"Can not resolve %s", hostnam);
  66.     error(msg);
  67.     }
  68.  
  69. #ifdef CRAY
  70.     /* server.sin_addr.s_addr = hostaddr; */
  71.     bcopy(blob->h_addr,(char *)&server.sin_addr,4);
  72. #else
  73.     /* bcopy(&hostaddr,(char *)&server.sin_addr,4); */
  74.     bcopy(blob->h_addr,(char *)&server.sin_addr,4);
  75. #endif
  76. #endif
  77.  
  78. /* fill in the address family and port number */
  79.     server.sin_family = AF_INET;
  80.     server.sin_port = portnum ? htons(portnum) : htons(PORT);
  81.  
  82. /* create socket */
  83.     sock = socket(AF_INET,SOCK_STREAM,0);
  84.     if (sock < 0)
  85.     error("couldn't create socket");
  86.  
  87. /* get name and role */
  88.     getpname(name,MAXNAME);
  89.     role = getrole();
  90.  
  91. /* tell user we're trying */
  92.     fprintf(stderr,"Trying %s ...\n",inet_ntoa(server.sin_addr));
  93.  
  94. /* connect to monitor.
  95.  * If this fails, first check that you're using the right library -- ASK
  96.  * AN EXPERT FOR YOUR SYSTEM.  Note that getting "Connection refused"
  97.  * is normal if the monitor isn't runing -- it's no action or a timeout
  98.  * that signifies a real problem.
  99.  */
  100.     ERRNO = 0;
  101.     if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
  102.     if (ERRNO > 0) perror("** failed to connect");
  103.     if (ERRNO == ECONNREFUSED)
  104.         sprintf(msg,"the monitor isn't running at %s",hostnam);
  105.     else
  106.         sprintf(msg,"couldn't connect to %s",hostnam);
  107.     error(msg);
  108.     }
  109.     fprintf(stderr,"-- Connected to %s --\n",hostnam);
  110.  
  111. /* assign input and output file descriptors */
  112.     outfd = sock;
  113.     infd = sock;
  114.  
  115.     p = name;
  116.     while(*p && *p != '\n') p++;
  117.     *p = '\0';
  118.  
  119. /* send header with buyer/seller/either role, human/machine, and name   */
  120.     sprintf(msg,"DA %d %d %8s\n",role,hmtype,name);
  121.     WRITE(outfd,msg,strlen(msg));
  122. /*    WRITE(outfd,name,strlen(name)); */
  123.     getpassword(password,MAXNAME);
  124.     WRITE(outfd,password,strlen(password));
  125.  
  126. /* echo messages from the monitor until 'start' or 'abort' or 'nogame' */
  127. #ifdef VMS
  128.     if (hmtype==1) putchar('\n');
  129. #endif
  130.     while (1) {
  131.     mptr = msg;
  132.     while (READ(infd,mptr,1) == 1 && *mptr != '\n') mptr++;
  133.     if (*mptr != '\n')
  134.         error("connection broken");
  135.     *++mptr = EOS;
  136.     if (strcmp(msg,"gamedata\n") == 0) {
  137.         get_gamedata(infd);
  138.         continue;
  139.     }
  140.     if (strcmp(msg,"players\n") == 0) {
  141.         get_players(infd);
  142.         continue;
  143.     }
  144.     if (strcmp(msg,"gamehist\n") == 0) {
  145.         get_gamehist(infd);
  146.         continue;
  147.     }
  148.     if (strcmp(msg,"start\n") == 0) break;
  149.     if (strcmp(msg,"abort\n") == 0 || strcmp(msg,"nogame\n") == 0) {
  150.         if (!dsply) {
  151.         if (*msg == 'a')
  152.             error("game aborted by monitor");
  153.         else
  154.             error("no game being played");
  155.         }
  156.         return 1;
  157.     }
  158.     if (dsply)
  159.         fputs(msg,stderr);
  160.     }
  161.     return 0;
  162. }
  163.